#!/bin/sh

touch emboslab-ref.sgml
touch makefile
chmod +w emboslab-ref.sgml 
chmod +w makefile

sgmldir="`dirname $0`"
doclist="${sgmldir}/doclist"
toplvl="${sgmldir}/.."

cat > makefile <<EOF
#=============================================================================
#
#    makefile
#
#    For building the eMBosLab docs
#
#=============================================================================
# ####EMBOSLABGPLCOPYRIGHTBEGIN####                                             
# -------------------------------------------                               
# This file is part of eMBosLab, the Embedded Configurable Operating System.    
# Copyright (C) 2011 Free Software Foundation, Inc.
#
# eMBosLab is free software; you can redistribute it and/or modify it under     
# the terms of the GNU General Public License as published by the Free      
# Software Foundation; either version 2 or (at your option) any later       
# version.                                                                  
#
# eMBosLab is distributed in the hope that it will be useful, but WITHOUT       
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     
# for more details.                                                         
#
# You should have received a copy of the GNU General Public License         
# along with eMBosLab; if not, write to the Free Software Foundation, Inc.,     
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             
#
# As a special exception, if other files instantiate templates or use       
# macros or inline functions from this file, or you compile this file       
# and link it with other works to produce a work based on this file,        
# this file does not by itself cause the resulting work to be covered by    
# the GNU General Public License. However the source code for this file     
# must still be made available in accordance with section (3) of the GNU    
# General Public License v2.                                                
#
# This exception does not invalidate any other reasons why a work based     
# on this file might be covered by the GNU General Public License.          
# -------------------------------------------                               
# ####EMBOSLABGPLCOPYRIGHTBEGIN####                                               
#=============================================================================
#####DESCRIPTIONBEGIN####
#
# Author(s):     Ricky Wu <rickleaf.wu@gmail.com>
# Date:          2011-03-22
#####DESCRIPTIONEND####
#=============================================================================

TOPLEVEL   := ${toplvl}/doclists
EOF
/bin/echo MAIN_SGML   := emboslab-ref.sgml >> makefile
/bin/echo -n OTHER_SGML   :=  >> makefile

cat >emboslab-ref.sgml <<EOF
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V3.1//EN" [

<!-- Begin Document Specific Declarations -->
<!ENTITY ui "&micro;ITRON">
<!ENTITY versiondir CDATA "v2_0">
<!ENTITY version CDATA "2.0">
<!ENTITY lt SDATA "[lt    ]">
EOF

PACKAGEVER=${1:-'current'}

for i in `cat ${doclist}`; do
  line="`echo $i | sed 's/#.*//;'`"
  if (test ! -z $line); then
   /bin/echo -n " ${toplvl}/doclists/$i" | sed "s@/current/@/${PACKAGEVER}/@g" >> makefile
   entityi="`echo $i | sed 's@\+@x@g; s@/@-@g; s@\.@-@g; s@_@-@g; s@-current-doc@@g; s@-current@@g'`"
   echo "<!ENTITY $entityi SYSTEM \"${toplvl}/doclists/$i\">" | sed "s@/current/@/${PACKAGEVER}/@g" >> emboslab-ref.sgml
  fi
done

# Go through again but find GIFs and PNGs to copy
for i in `for j in \`cat ${doclist}\` ; do dirname $j ; done | sort | uniq` ; do
  diri=${toplvl}/doclists/pix
  dirigifs=`ls $diri/*.gif $diri/*.png 2>/dev/null | tr '\n' ' '`
  if [ x"$dirigifs" != x ]; then
        copyfiles="$copyfiles $dirigifs" 
  fi
done

cat >> makefile <<EOF

MAIN_HTML  := emboslab-ref.html
MAIN_PDF   := emboslab-ref.pdf
PICTURES   :=
COPYFILES  := $copyfiles

include \$(TOPLEVEL)/docconf/rules.doc
EOF

cat >> emboslab-ref.sgml <<EOF
<!-- End Document Specific Declarations -->
]>

<BOOK ID="EMBOSLAB-REF">
  <bookinfo>
    <TITLE>eMBosLab Reference Manual</TITLE>

    <copyright>
      <year>2011</year>
      <holder>Free Software Foundation, Inc.</holder>
    </copyright>

    <legalnotice>
	<title>Documentation licensing terms</title>
<para>This material may be distributed only subject to the terms
and conditions set forth in the Open Publication License, v1.0
or later (the latest version is presently available at
<ulink url="http://www.opencontent.org/openpub/">http://www.opencontent.org/openpub/</ulink>).
</para>
<para>
Distribution of substantively modified versions of this
document is prohibited without the explicit permission of the
copyright holder.</para>
<para>
Distribution of the work or derivative of the work in any
standard (paper) book form is prohibited unless prior
permission is obtained from the copyright holder.
</para>
<para>eMBosLab are open source software, covered by a
modified version of the <ulink url="http://www.gnu.org/copyleft/gpl.html">GNU
General Public Licence</ulink>,
and you are welcome to change it and/or distribute copies of it under certain
conditions.</para>
      </legalnotice>
  </bookinfo>
<toc id="emboslab-toc"></toc>
EOF

for i in `cat ${doclist}`; do
  line="`echo $i | sed 's/#.*//;'`"
  if (test ! -z $line); then
   entityi="`echo $i | sed 's@\+@x@g; s@/@-@g; s@\.@-@g; s@_@-@g; s@-current-doc@@g; s@-current@@g'`"
   # special kludge
   if [ "`basename $i`" != "tcpip-manpages.sgml" -a \
	"`basename $i`" != "snmp-manpages.sgml" -a \
	"`basename $i`" != "libipsec.sgml" -a \
	"`basename $i`" != "libipsec-manpages.sgml" -a \
	"`basename $i`" != "openbsd-manpages-bridge.sgml" -a \
	"`basename $i`" != "openbsd-manpages-netintro.sgml" -a \
	"`basename $i`" != "openbsd-manpages-stp.sgml" -a \
	"`basename $i`" != "porting.sgml" ]; then
     echo "&$entityi;" >> emboslab-ref.sgml
   fi
  fi
done

echo '</book>' >> emboslab-ref.sgml
